import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;

import javax.swing.*;
import javax.swing.text.html.HTMLDocument.Iterator;

public class Console extends JInternalFrame implements ActionListener {
	
	private JTextField commandInput;
	private ArrayList<String> groups;
	private JTextArea commandOutput;
	private HashMap<String, ArrayList<String>> groupMemberMap;
	private MultiThreadingServer server;
	private Semaphore mutex;
	private GroupDataInterface groupData;

	public Console(MultiThreadingServer server, Semaphore mutex,  GroupDataInterface groupData) {
		
		super("Console", true, true, true, true);	
		this.groupData = groupData;
		this.groupMemberMap = new HashMap();
		this.server = server;
		this.mutex = mutex;
		this.groups = groups;
		this.setupGUI();
	}

	public void setupGUI() {
		
		this.commandInput = new JTextField(20);
		this.commandInput.addActionListener(this);			
		this.commandOutput = new JTextArea(19,70);		
		this.commandOutput.setEditable(false);	
		this.commandOutput.setForeground(Color.GREEN);
		
		JScrollPane scrollPane = new JScrollPane(commandOutput,
		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);		
		
		GridBagLayout gridBag = new GridBagLayout();
		Container contentPane = getContentPane();
		contentPane.setLayout(gridBag);
		
		GridBagConstraints gridBagConstraintsx1 = new GridBagConstraints();
        gridBagConstraintsx1.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraintsx1.fill = GridBagConstraints.HORIZONTAL;
        contentPane.add(commandInput, gridBagConstraintsx1);
        
        GridBagConstraints gridBagConstraintsx2 = new GridBagConstraints();
        gridBagConstraintsx2.weightx = 600;
        gridBagConstraintsx2.weighty = 600;
        contentPane.add(scrollPane, gridBagConstraintsx2);	
       
		this.setVisible(true);	
	}

	public void actionPerformed(ActionEvent evt) {
		
		String text = commandInput.getText();
		
		if(text.compareTo("clear") == 0)
			commandOutput.setText("");
		
		else if(text.compareTo("show all groups") == 0) {
			commandOutput.setText("");
			
			if(this.groupData.getNumberOfGroups() == 0)
				commandOutput.setText("Number of groups: 0");
			
			else {
				commandOutput.setText("Number of groups: "+this.groupData.getNumberOfGroups()+"\n\n");
				ArrayList<String> groupList = this.groupData.getAllGroups();
				
				for(int i=0; i < groupList.size(); i++)
					commandOutput.append("Group: "+groupList.get(i)+"\n");
				
					
				
				
			}
			
		}
		
		else if(text.matches("show all members from .*") == true) {			
			
			String[] inputSplit = text.split(" ");
			
			if(inputSplit.length == 5) {
			
				commandOutput.setText("Members from group: "+inputSplit[4]+"\n\n");
				
				if(this.groupData.existGroup(inputSplit[4]) == true) {
					ArrayList<String> memberList = this.groupData.getMemberNamesByGivenGroup(inputSplit[4]);
					for(int i=0; i < memberList.size(); i++)
						if(this.groupData.isMaster(memberList.get(i)))
							commandOutput.append("Member: "+memberList.get(i)+" (master)\n");	
					
					else
						commandOutput.append("Member: "+memberList.get(i)+"\n");
				}
				
				else
					commandOutput.append("Group: "+inputSplit[4]+" doesent exists\n");
						
			}
			
			else 
				commandOutput.setText("Groupname is empty\n");
			
		}
		
		else if(text.compareTo("show all members") == 0) {
			commandOutput.setText("");
			
			if(this.groupData.getNumberOfGroups() == 0)
				commandOutput.setText("No members online");
			else {
				
				commandOutput.setText("All Members\n\n");
				
				Collection<ArrayList<String>> memberCollection = this.groupData.getAllMembers();
				
				java.util.Iterator<ArrayList<String>> iter = memberCollection.iterator();
				
				while(iter.hasNext()) {
					
					ArrayList<String> memberList = iter.next();
					
					for(int i=0; i < memberList.size(); i++) {
						
						if(this.groupData.isMaster(memberList.get(i)))
								commandOutput.append("Member: "+memberList.get(i)+" (master)\n");	
						
						else
							commandOutput.append("Member: "+memberList.get(i)+"\n");
					}
				}
				
			}							
			
			
		}
		
		else if(text.compareTo("show all members from") == 0) {
			commandOutput.setText("");
			
		}
		
		else if(text.compareTo("help") == 0) {
			commandOutput.setText("");
			this.printHelp();			
		}
		
		else if(text.compareTo("status") == 0) {
			commandOutput.setText("Number of transfered bytes: "+this.server.getNumberOfBytesTransfered());			
		}
		
		else if(text.compareTo("") == 0) 
			commandOutput.setText("");
		
				
		else {
			commandOutput.setText("");
			commandOutput.append("unkown command, try help for more informations");
			commandInput.selectAll();
		}
		
		this.commandInput.setText("");
	}
	
	
	public void printHelp() {	
		
		commandOutput.append("Help\n");
		commandOutput.append("[show all groups]  - show all available groups\n");	
		commandOutput.append("[show all members] - show all available members\n");
		commandOutput.append("[show all members from <group>] - show all available members  from <group>\n");
		commandOutput.append("[show master from <group>] - show current master from <group>\n");
		commandOutput.append("[status] - print server status\n");
		commandOutput.append("[clear] - clear console\n");
		commandOutput.append("[help] - print help information\n");
	}
	
	
	  
}


